<?php
	// Search and modify illegal portions
	$_POST = str_replace("'","''",$_POST);
	$_POST = str_replace("\\","\\\\",$_POST);
	include("write_functions.php");
	connect("localhost", "root", "", "gradegriddatabase");
	
	// Search for unmodifiable errors
	$clean = true;
	$error = "<div align=\"center\">";
	$error = $error . "The information you submitted have the following errors:<br/><div style='color:#F00'>";
	
	// Check first name
	if(strlen($_POST['fname'])==0){
		$error = $error . "First name is unspecified.<br/>";
		$clean = false;
	}
	
	// Check last name
	if(strlen($_POST['lname'])==0){
		$error = $error . "Last name is unspecified.<br/>";
		$clean = false;
	}
	
	// Check email
	if(strlen($_POST['email'])==0){
		$error = $error . "Email is unspecified.<br/>";
		$clean = false;
	}
	
	// Check if email is registered
	$que = "SELECT *
			FROM account
			WHERE email = '".$_POST['email']."'";
	$result = mysql_query($que);
	if(mysql_num_rows($result)!=0){
		$error = $error . "Email is already registered.<br/>";
		$clean = false;
	}
	
	// Create username
	$user = $_POST['fname'][0];
	$user = $user . $_POST['lname'];
	
	// Check for duplicates
	$que = "SELECT *
			FROM account
			WHERE username = '".$user."'";
	$result = mysql_query($que);
	$i = 1;
	while(mysql_num_rows($result)!=0){
		$user2 = $user . $i;
		$que = "SELECT *
				FROM account
				WHERE username = '".$user2."'";
		$result = mysql_query($que);
		$i++;
	}
	
	$error = $error . "</div>Please check the information you have submitted.<br/>";
?>